home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / glchess / history.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  4.7 KB  |  193 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __author__ = 'Robert Ancell <bob27@users.sourceforge.net>'
  5. __license__ = 'GNU General Public License Version 2'
  6. __copyright__ = 'Copyright 2005-2006  Robert Ancell'
  7. import os
  8. import errno
  9. import chess.pgn as chess
  10. from defaults import *
  11.  
  12. class GameHistory:
  13.     
  14.     def __init__(self):
  15.         
  16.         try:
  17.             os.makedirs(HISTORY_DIR)
  18.         except OSError:
  19.             e = None
  20.             if e.errno != errno.EEXIST:
  21.                 print 'Failed to make history directory: %s' % e.strerror
  22.             
  23.         except:
  24.             e.errno != errno.EEXIST
  25.  
  26.  
  27.     
  28.     def getUnfinishedGame(self):
  29.         '''Get the last game that is unfinished.
  30.         
  31.         Returns the (PGN game, fileName, inHistory) or (None, None, False) if no unfinished games.
  32.         '''
  33.         g = None
  34.         fileName = None
  35.         inHistory = False
  36.         
  37.         try:
  38.             f = file(UNFINISHED_FILE, 'r')
  39.             lines = f.readlines()
  40.             f.close()
  41.             index = 0
  42.             while len(lines) > 0:
  43.                 fileName = lines[0].strip()
  44.                 
  45.                 try:
  46.                     p = chess.pgn.PGN(fileName, 1)
  47.                 except chess.pgn.Error:
  48.                     e = None
  49.                     print e.message
  50.                 except IOError:
  51.                     e = None
  52.                     print e.strerror
  53.  
  54.                 result = p[0].getTag(chess.pgn.TAG_RESULT)
  55.                 if result == chess.pgn.RESULT_INCOMPLETE:
  56.                     g = p[0]
  57.                     inHistory = fileName.startswith(HISTORY_DIR)
  58.                     break
  59.                 
  60.                 lines = lines[1:]
  61.         except IOError:
  62.             e = None
  63.             if e.errno != errno.ENOENT:
  64.                 print e.errno
  65.                 print 'Failed to read unfinished list'
  66.                 return (None, None, False)
  67.             lines = []
  68.         except:
  69.             e.errno != errno.ENOENT
  70.  
  71.         
  72.         try:
  73.             f = file(UNFINISHED_FILE, 'w')
  74.             f.writelines(lines)
  75.             f.close()
  76.         except IOError:
  77.             e.errno != errno.ENOENT
  78.             e.errno != errno.ENOENT
  79.             print 'Failed to write unfinished list'
  80.         except:
  81.             e.errno != errno.ENOENT
  82.  
  83.         return (g, fileName, inHistory)
  84.  
  85.     
  86.     def load(self, date):
  87.         pass
  88.  
  89.     
  90.     def _getFilename(self, game):
  91.         date = game.getTag(chess.pgn.TAG_DATE)
  92.         
  93.         try:
  94.             (year, month, day) = date.split('.')
  95.         except ValueError:
  96.             directory = HISTORY_DIR
  97.  
  98.         directory = os.path.join(HISTORY_DIR, year, month, day)
  99.         
  100.         try:
  101.             os.makedirs(directory)
  102.         except OSError:
  103.             e = None
  104.             if e.errno != errno.EEXIST:
  105.                 return None
  106.         except:
  107.             e.errno != errno.EEXIST
  108.  
  109.         count = 0
  110.         fileName = os.path.join(directory, date)
  111.         while os.path.exists(fileName):
  112.             count += 1
  113.             fileName = os.path.join(directory, '%s-%d' % (date, count))
  114.             continue
  115.             e.errno != errno.EEXIST
  116.         return fileName
  117.  
  118.     
  119.     def rename(self, oldName, newName):
  120.         
  121.         try:
  122.             os.unlink(oldName)
  123.         except OSError:
  124.             print 'Failed to remove game from history'
  125.  
  126.         
  127.         try:
  128.             f = file(UNFINISHED_FILE, 'r')
  129.             lines = f.readlines()
  130.             f.close()
  131.             f = file(UNFINISHED_FILE, 'w')
  132.             for line in lines:
  133.                 l = line.strip()
  134.                 if l == oldName:
  135.                     f.write(newName + '\n')
  136.                     continue
  137.                 f.write(l + '\n')
  138.             
  139.             f.close()
  140.         except IOError:
  141.             print 'Failed to update unfinished list'
  142.  
  143.  
  144.     
  145.     def save(self, g, fileName):
  146.         """Save a game in the history.
  147.         
  148.         'g' is the game to save
  149.         'fileName' is the history file to write to or None to create a new one
  150.         """
  151.         if fileName is None:
  152.             fileName = self._getFilename(g)
  153.             if fileName is None:
  154.                 print 'Unable to find location to save to'
  155.                 return None
  156.         
  157.         lines = g.getLines()
  158.         
  159.         try:
  160.             f = file(fileName, 'w')
  161.             for line in lines:
  162.                 f.write(line + '\n')
  163.             
  164.             f.write('\n')
  165.             f.close()
  166.         except IOError:
  167.             e = None
  168.             print 'Unable to autosave to %s: %s' % (fileName, str(e))
  169.  
  170.         result = g.getTag(chess.pgn.TAG_RESULT)
  171.         
  172.         try:
  173.             f = file(UNFINISHED_FILE, 'r')
  174.             lines = f.readlines()
  175.             f.close()
  176.             f = file(UNFINISHED_FILE, 'w')
  177.             if result == chess.pgn.RESULT_INCOMPLETE:
  178.                 f.write(fileName + '\n')
  179.             
  180.             for line in lines:
  181.                 l = line.strip()
  182.                 if l == fileName and result == chess.pgn.RESULT_INCOMPLETE:
  183.                     continue
  184.                 
  185.                 f.write(l + '\n')
  186.             
  187.             f.close()
  188.         except IOError:
  189.             print 'Failed to update unfinished list'
  190.  
  191.  
  192.  
  193.